package com.springboot.mybatis.service.collectiondemo;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetIterator {
    public static void main(String[] args) {
        //使用默认构造器，则默认元素升序排列
        TreeSet treeSet = new TreeSet();
        treeSet.add("11");
        treeSet.add("22");
        treeSet.add("77");
        treeSet.add("44");
        treeSet.add("55");
        ascIteratorThroughIterator(treeSet);
        descIteratorThroughIterator(treeSet);
    }

    public static void ascIteratorThroughIterator(TreeSet treeSet){
        System.out.println("升序迭代输出：");
        for (Iterator iterator = treeSet.iterator(); iterator.hasNext();){
            System.out.printf((String) iterator.next()+" ");
        }
        System.out.println();
    }

    public static void descIteratorThroughIterator(TreeSet treeSet){
        System.out.println("降序迭代输出：");
        for (Iterator iterator = treeSet.descendingIterator();iterator.hasNext();){
            System.out.printf((String) iterator.next()+" ");
        }
    }
}
